package com.wuyou.designPatterns.inversion.improve;

/**
 * @Description 以来倒置原则
 * @Author 无忧
 * @Date 2024/5/27 23:00
 */
public class DependecyInversion {


    public static void main(String[] args) {
        Person person = new Person();
        person.receive(new Email());
        person.receive(new Weixin());
    }
}


interface IReceiver{
    public String getInfo();
}

class Email implements IReceiver{
    @Override
    public String getInfo(){
        return "email: hello world!";
    }
}
class Weixin implements IReceiver{
    @Override
    public String getInfo(){
        return "email: hello oppenid!";
    }
}

// 方式1
// 1、简单，比较容易想到
// 2、如果获取的对象是微信、短信等，则新增类同时Person也要增加相应得到接受方法
// 3、解决思路：引入一个抽象的接口IReceiver，表示接收者，这样Person类与接口IReceiver发生依赖
//  因为Email，微信等等属于接收的范围，他们各自实现IReceiver接口就ok，这样就符合依赖倒转原则
class Person{
    public void receive(IReceiver receiver){
        System.out.println(receiver.getInfo());
    }
}
